package com.leyou.trade.utils;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.local.UserContext;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Data
@Component
public class CollectionNameBuilder {

    @Value("${ly.mongo.collectionNamePrefix}")
    private String collectionNamePrefix;

    public String calculate() {
        //从上下文中获取用户信息
        UserDetails user = UserContext.getUser();
        //判断是否为空
        if (user == null) {
            return "";
        }
        //获取用户id
        Long id = user.getId();
        //计算hash值并对100取模
        int index = id.hashCode() % 100;
        //返回
        return collectionNamePrefix + index;
    }
}
